package com.czy.daishusys.ui.personal;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.czy.daishusys.R;
import com.czy.daishusys.adapter.PersonPageAdapter;
import com.czy.daishusys.db.UserHelper;
import com.czy.daishusys.entity.PersonPageItem;
import com.czy.daishusys.global.Constants;
import com.czy.daishusys.ui.BaseActivity;
import com.czy.daishusys.ui.login.LoginActivity;
import com.czy.daishusys.utils.ActivityHelper;
import com.czy.daishusys.utils.PicDialog;
import com.czy.daishusys.utils.SharedPreferencesUtils;
import com.czy.daishusys.utils.UniversalDialog;
import com.czy.daishusys.widget.RecycleViewDivider;
import com.hyphenate.chat.EMClient;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class AccountSettingsActivity extends BaseActivity {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_subtitle)
    TextView toolbarSubtitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.btn_login_out)
    Button btnLoginOut;

    public static final int NICK_NAME = 1;
    private static final String[] TITLE = {"登录账号","昵称","修改密码"};
    private static final int[] IMAGE = {R.mipmap.ic_account_blue_bg,R.mipmap.ic_nick_name,R.mipmap.ic_passwd_orange};
    private ArrayList<PersonPageItem> mDataList = new ArrayList<>();

    private BaseQuickAdapter mAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_account_settings;
    }

    @Override
    protected void initInjector() {
        getToolbarTitle().setText(R.string.account_settings_title);
    }

    @Override
    protected void initViews() {
        /*recyclerView初始化，并禁止滑动*/
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        initData();
        initAdapter();
    }

    @OnClick(R.id.btn_login_out)
    protected void OnLoginOut(){
        showLoginOutDialog("退出登录", "退出后不会删除任何数据,下次登录依然可以使用本账号");
    }


    private void initData(){
        String[] CONTENT = new String[3];
        String phone = UserHelper.getInstance().getUserInfoModel(this).getPhone();
        String nickName = UserHelper.getInstance().getUserInfoModel(this).getNickName();
        CONTENT[0] = phone == null ? "" : phone ;
        CONTENT[1] = nickName == null ? "" : nickName;
        CONTENT[2] = "";
        for(int i = 0 ; i < 3 ; ++i){
            PersonPageItem item = new PersonPageItem();
            item.setName(TITLE[i]);
            item.setIconId(IMAGE[i]);
            item.setContent(CONTENT[i]);
            mDataList.add(item);
        }
        mDataList.get(0).setHasArrow(false);//第一行没有有箭头
    }

    private void initAdapter(){
        mAdapter = new PersonPageAdapter(R.layout.person_page_item_view, mDataList);
//        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 1 ,R.color.appThemeBorderColor));
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (position){
                    case 0://登录账号
                        break;
                    case 1://昵称
                        AccountSettingsActivity.this.startActivityForResult(new Intent(AccountSettingsActivity.this , EditNickNameActivity.class),NICK_NAME);
                        break;
                    case 2://修改密码
                        AccountSettingsActivity.this.startActivity(new Intent(AccountSettingsActivity.this , ModifyPasswordActivity.class));
                        break;
                }
            }
        });

        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case NICK_NAME:
                if(data!=null){
                    String nickName = data.getStringExtra("nick_name");
                    mDataList.get(1).setContent(""+nickName);
                    mAdapter.notifyDataSetChanged();
                }
                break;
        }
    }

    @Override
    protected void updateViews() {

    }

    private void showLoginOutDialog(String title, String msg) {

        PicDialog.Builder builder = new PicDialog.Builder(AccountSettingsActivity.this);
        builder.setTitleIcon(R.mipmap.ic_dialog_title_login_out)
                .setTitle(title)
                .setMessage(msg)
                .setNegativeIcon(R.mipmap.ic_dialog_cancel)
                .setPositiveIcon(R.mipmap.ic_dialog_ensure)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                         /*清除登录信息*/
                        UserHelper.getInstance().setDevicePid("");
                        SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_SESSION_INFO_MODEL);
                        SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_USER_INFO_MODEL);
                        SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_ADMIN_INFO_MODEL);
                        ActivityHelper.getInstance().finishAllActivity();
                        UserHelper.getInstance().setUserInfoModel(AccountSettingsActivity.this, null);
                        UserHelper.getInstance().setSessionInfoModel(AccountSettingsActivity.this, null);
                        UserHelper.getInstance().setAdminInfoModel(AccountSettingsActivity.this, null);
                        UserHelper.getInstance().setIsLogined(false);
                        EMClient.getInstance().logout(true);
                        LoginActivity.navToLogin(AccountSettingsActivity.this);
                    }
                });
        builder.create().show();
    }
}
